package com.ruoyi.erp.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.BusinessUnitDepartment;
import com.ruoyi.erp.service.IBusinessUnitDepartmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 成本中心Controller
 * 
 * @author sauger
 * @date 2022-04-03
 */
@Controller
@RequestMapping("/erp/businessUnit/department")
public class BusinessUnitDepartmentController extends BaseController
{
    private String prefix = "erp/businessUnit/department";

    @Autowired
    private IBusinessUnitDepartmentService businessUnitDepartmentService;

    @RequiresPermissions("erp:department:view")
    @GetMapping()
    public String department()
    {
        return prefix + "/index";
    }

    /**
     * 查询成本中心列表
     */
    @RequiresPermissions("erp:department:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessUnitDepartment businessUnitDepartment)
    {
        startPage();
        List<BusinessUnitDepartment> list = businessUnitDepartmentService.selectBusinessUnitDepartmentList(businessUnitDepartment);
        return getDataTable(list);
    }

    /**
     * 导出成本中心列表
     */
    @RequiresPermissions("erp:department:export")
    @Log(title = "成本中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessUnitDepartment businessUnitDepartment)
    {
        List<BusinessUnitDepartment> list = businessUnitDepartmentService.selectBusinessUnitDepartmentList(businessUnitDepartment);
        ExcelUtil<BusinessUnitDepartment> util = new ExcelUtil<BusinessUnitDepartment>(BusinessUnitDepartment.class);
        return util.exportExcel(list, "成本中心数据");
    }

    /**
     * 新增成本中心
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存成本中心
     */
    @RequiresPermissions("erp:department:add")
    @Log(title = "成本中心", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusinessUnitDepartment businessUnitDepartment)
    {
        return toAjax(businessUnitDepartmentService.insertBusinessUnitDepartment(businessUnitDepartment));
    }

    /**
     * 修改成本中心
     */
    @RequiresPermissions("erp:department:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        BusinessUnitDepartment businessUnitDepartment = businessUnitDepartmentService.selectBusinessUnitDepartmentById(id);
        mmap.put("businessUnitDepartment", businessUnitDepartment);
        return prefix + "/edit";
    }

    /**
     * 修改保存成本中心
     */
    @RequiresPermissions("erp:department:edit")
    @Log(title = "成本中心", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessUnitDepartment businessUnitDepartment)
    {
        return toAjax(businessUnitDepartmentService.updateBusinessUnitDepartment(businessUnitDepartment));
    }

    /**
     * 删除成本中心
     */
    @RequiresPermissions("erp:department:remove")
    @Log(title = "成本中心", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(businessUnitDepartmentService.deleteBusinessUnitDepartmentByIds(ids));
    }
}
